<?php

namespace App\Models\Landscaping;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ParkManintenance extends Model
{
    use SoftDeletes;
    protected $guarded = [];

    //查询list
    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
    	return self::orderBy('id','desc')
                ->with(['department','parkVegetation'])
                ->whereHas('parkVegetation',function($query) use ($request){
                    //检测当前的请求中是否包含name参数
                    $name = $request->input('name','');//植被名称
                    if(!empty($name)) {
                        $query->where('name','like','%'.$name.'%');
                    }
                    $query->with('vegetation');
                })
                ->where(function ($query) use ($request){
                    $department_id = $request->input('department_id','');
                    if(!empty($department_id)) {
                        $query->where('department_id','=',$department_id);
                    }
                })
                ->paginate($per_page);
    }

    //查询所属部门
    public function department()
    {
    	return $this->belongsTo(\App\Models\SystemManagement\Department::class,'department_id','id')->select('id','name');
    }

    //获取植被信息
    public function parkVegetation()
    {
        return $this->belongsTo(\App\Models\Landscaping\ParkVegetation::class,'vegetation_id','id')->select('id','name', 'vegetation_id');
    }

}
